package com.ornate.core.provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.ContextLoader;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public class ActionSupportProvider extends ActionSupport {

	// Properties
	private static final long serialVersionUID = -8897838864164111221L;

	private Object jsonObject;

	public Map<String, Object> params = getParameterMap();

	public static final String JSON_STRING = "json", JSON_ARRAY = "json", JSON_OBJECT = "json", JSON_RESULT = "json", JSON_EXCEPTION = "json";

	// Methods
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpSession getSession(boolean create) {
		return getRequest().getSession(create);
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public ServletContext getApplication() {
		return getServletContext();
	}

	public String getRealPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public Object getBean(Class cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Object getBean(String cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	public Map<String, Object> getParameterMap() {
		Map<String, Object> params = new HashMap<String, Object>(getRequest().getParameterMap());
		Entry<String, Object> entry;
		String[] value;
		for (Iterator<Entry<String, Object>> iterator = params.entrySet().iterator(); iterator.hasNext();) {
			entry = iterator.next();
			value = ((String[]) entry.getValue());
			if (value.length == 1) {
				entry.setValue(value[0]);
				continue;
			}
			if (value.length == 0) {
				params.remove(entry.getKey());
				continue;
			}
			// ## FIXME DEBUG ##
			List<String> values = new ArrayList<String>();
			for (int i = 0, len = value.length; i < len; i++) {
				values.add(value[i]);
			}
			entry.setValue(values);
			// ## DEBUG ##
		}
		return params;
	}

	// Getters and Setters
	public Object getJSONObject() {
		return jsonObject;
	}

	public void setJSONObject(Object jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setJSONPagingResult(Object data, int total) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", data);
		result.put("total", total);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(boolean successful) {
		setJSONProcessResult(successful, null);
	}

	public void setJSONProcessResult(boolean successful, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", successful);
		if (message != null) result.put("message", message);
		this.jsonObject = result;
	}

	public void setJSONProcessResult(Map<String, Object> result) {
		this.jsonObject = result;
	}

}
